<template>
  <div class="cs-page-container">
    <div class="cs-filter-container">
      <el-input
        v-if="!userId"
        v-model.trim="listQuery.userId"
        clearable
        placeholder="用户ID"
        style="width:100px"
      />
      <el-select
        v-if="!propType"
        v-model="listQuery.propType"
        clearable
        multiple
        filterable
        placeholder="属性类型"
        style="width:100px"
      >
        <el-option
          v-for="option in types"
          :key="option.value"
          :label="option.label"
          :value="option.value"
        />
      </el-select>
      <el-date-picker
        v-model="daterangeValue"
        :picker-options="daterangePickerOptions"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="创建起始时间"
        end-placeholder="创建结束时间"
        value-format="yyyy/MM/dd"
        @change="daterangeChangedTimestamp"
      />
      <el-button
        type="primary"
        size="small"
        icon="el-icon-search"
        @click="handleFilter"
      >搜索</el-button>
      <el-button
        type="info"
        size="small"
        icon="el-icon-refresh-right"
        @click="resetFilter"
      >重置</el-button>
    </div>

    <div class="cs-filter-result">
      <el-table
        v-loading="listLoading"
        :data="list"
        :height="'calc(100vh - 250px)'"
        border
        fit
        size="small"
        highlight-current-row
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
      >
        <el-table-column align="center" label="用户ID" prop="uid">
          <template slot-scope="{ row }">
            <TooptipsCom :user-id="row.uid" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="属性">
          <template slot-scope="{ row }">{{ row.propType }}</template>
        </el-table-column>
        <el-table-column align="center" label="增加值">
          <template slot-scope="{ row }">{{ propChange(row, row.addChange) }}</template>
        </el-table-column>
        <el-table-column align="center" label="减少值">
          <template slot-scope="{ row }">{{ propChange(row, row.reduceChange) }}</template>
        </el-table-column>
      </el-table>
      <TablePagination
        :current-page="listQuery.page"
        :limit="listQuery.limit"
        :total="total"
        :loading="listLoading"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChangeOffset"
      />
    </div>
  </div>
</template>

<script>
import TablePagination from '@/components/TablePagination'

import { xhrPropChangeCount, xhrPropChangeOptions } from '@/api/account'
export default {
  name: 'PropChangeList',
  components: { TablePagination },
  props: {
    userId: {
      type: Number,
      default: null
    },
    propType: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      daterangeValue: '',
      listQuery: {
        page: 1,
        limit: 100,
        startTime: null,
        endTime: null,
        userId: (this.$route.query && this.$route.query.id) || null,
        propType: []
      },
      listLoading: true,
      dialogVisible: false,
      types: []
    }
  },
  created() {
    xhrPropChangeOptions().then(resp => {
      this.types = resp.data.types
      this.doSearch()
    })
  },
  methods: {
    propChange(row, value) {
      if (row.propType === '钻石') {
        return value / 10
      } else if (row.propType === '积分+') {
        return value / 100
      } else {
        return value
      }
    },
    doSearch() {
      if (this.listQuery.userId && !/^\d*$/.test(this.listQuery.userId)) {
        this.$message.warning('用户ID只能是数字')
        this.listLoading = false
        return
      }
      if (this.propType) {
        this.listQuery.propType = [this.propType]
      }
      if (this.userId) {
        this.listQuery.userId = this.userId
      }
      const params = {
        userId: this.listQuery.userId,
        offset: this.listQuery.offset,
        limit: this.listQuery.limit,
        startTime: this.listQuery.startTime,
        endTime: this.listQuery.endTime,
        propType: (this.listQuery.propType && this.listQuery.propType.join(',')) || null
      }
      xhrPropChangeCount(params)
        .then(resp => {
          this.list = resp.data.list
          this.total = resp.data.count
          this.listLoading = false
        })
        .catch(err => {
          this.$message.error(err)
          this.listLoading = false
        })
    },
    onGoDetail(id) {
      this.$router.push('/account/account-detail?id=' + id)
    },
    onForbid(row) {
      this.forbidAccounts = row.id
      this.dialogVisible = true
    }
  }
}
</script>
